﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows;
using Clipper.BLL;

namespace ClipperBaseUI
{
    public class UIHelper
    {
        static SystemLog mSystemLogBll = new SystemLog();
        public delegate ObservableCollection<Clipper.Model.AbstractModel> DataHandle();
        public delegate ObservableCollection<Clipper.Model.AbstractModel> DataWhereHandle(string str);

        /// <summary>
        /// 获得语言文件中某个键的值
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string GetResourceText(string strKey)
        {
            ResourceDictionary dic = Application.Current.Resources.MergedDictionaries[0] as ResourceDictionary;
            //if(dic!=null)
            //{
            return dic[strKey].ToString();
        }
        /// <summary>
        /// 获得语言文件中两个键值的组合
        /// </summary>
        /// <param name="key1"></param>
        /// <param name="key2"></param>
        /// <returns></returns>
        public static string GetResourceText(string key1, string key2)
        {
            ResourceDictionary dic = Application.Current.Resources.MergedDictionaries[0] as ResourceDictionary;
            return dic[key1].ToString() + dic[key2].ToString();
        }
        /// <summary>
        /// 操作提示
        /// </summary>
        /// <param name="result">成功或失败</param>
        /// <param name="operateKey">操作内容</param>
        public static void SuccessOrFailTip(bool result, string operateKey)
        {
            string operate = UIHelper.GetResourceText(operateKey);
            if (result)
            {
                MessageBox.Show(operate + UIHelper.GetResourceText("SuccessTip"));
            }
            else
            {
                MessageBox.Show(operate + UIHelper.GetResourceText("FailTip"));
            }
        }
        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="messageKey"></param>
        public static void ShowMessage(string messageKey)
        {
            MessageBox.Show(UIHelper.GetResourceText(messageKey));
        }
        /// <summary>
        /// 显示信息
        /// </summary>
        public static void ShowMessage(int messageCode)
        {
            string firstLetter = messageCode.ToString().Substring(0, 1);
            string strMessage = GetResourceText(messageCode.ToString());
            string level = "";
            switch (firstLetter)
            {
                case "1"://消息
                    level = GetResourceText("MessageTip");
                    break;
                case "2"://警告
                    level = GetResourceText("AlertTip");
                    MessageBox.Show(strMessage, level);
                    break;
                case "3"://错误
                    level = GetResourceText("ErrorTip");
                    MessageBox.Show(strMessage, level);
                    break;

            }
            //保存日志
            mSystemLogBll.Add(new Clipper.Model.SystemLog()
            {
                level = level,
                logDate = DateTime.Now,
                logDescribe = strMessage,
                title = messageCode.ToString()
            });
        }

        /// <summary>
        /// 获得初始化数据列表的backgroundworker
        /// </summary>
        /// <param name="bindMethod"></param>
        /// <param name="datagrid"></param>
        /// <param name="progressbar"></param>
        /// <returns></returns>
        public static BackgroundWorker InitDataGrid(DataWhereHandle bindMethod, DataGrid datagrid)
        {
            BackgroundWorker bgw = new BackgroundWorker();
            ObservableCollection<Clipper.Model.AbstractModel> dataList = null;
            bgw.DoWork += (ss, ee) => { dataList = bindMethod.Invoke(""); };
            bgw.RunWorkerCompleted += (ss, ee) =>
            {
                datagrid.ItemsSource = dataList;
            };
            return bgw;
        }
    }
}
